package com.yunduo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.RetunData.statuscode.impl.ResultCode;
import com.yunduo.common.RetunData.statuscode.impl.ServiceCode;
import com.yunduo.entity.ShopGoods;
import com.yunduo.entity.vo.ShopGoodsObj;
import com.yunduo.handler.exceptionhandler.OperationException;
import com.yunduo.service.ShopGoodsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  商店与商品关联控制器
 * </p>
 *
 * @author ChengXin
 * @since 2023-07-22
 */
@RestController
@RequestMapping("/shopGoods")
public class ShopGoodsController {

    @Resource
    private ShopGoodsService shopGoodsService;

    //商品添加到店铺
    @PostMapping("/add")
    public CommonReturn goodConnectionShop(@RequestBody ShopGoods data){
        LambdaQueryWrapper<ShopGoods> queryWrapper = buildQueryByShopAndGoodId(data);
        ShopGoods isExist = shopGoodsService.getOne(queryWrapper);
        if(isExist != null){
            return CommonReturn.ERROR(ServiceCode.IS_EXIST);
        }
        ShopGoods shopGoods = new ShopGoods();
        shopGoods.setGoodId(data.getGoodId());
        shopGoods.setShopId(data.getShopId());
        shopGoods.setSale(false); //默认未上架
        shopGoodsService.save(shopGoods);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //从店铺中删除商品
    @DeleteMapping("/delete")
    public CommonReturn deleteShopGood(@RequestBody ShopGoods shopGoods){
        LambdaQueryWrapper<ShopGoods> queryWrapper = buildQueryByShopAndGoodId(shopGoods);
        shopGoodsService.remove(queryWrapper);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //店铺批量删除商品
    @DeleteMapping("/deletes")
    public CommonReturn deleteShopGoods(@RequestBody ShopGoodsObj data) {
        if(StringUtils.isEmpty(data.getShopId()) || data.getGoodIds() == null || data.getGoodIds().length == 0){
            throw new OperationException(ServiceCode.PARAM_ERROR);
        }
        shopGoodsService.deleteByShopAndGoodIds(data.getShopId(),data.getGoodIds());
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //查询店铺的所有商品id
    @GetMapping("/get/{id}")
    public CommonReturn getShopGoods(@PathVariable("id") String shopId){
        LambdaQueryWrapper<ShopGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoods::getShopId,shopId);
        List<ShopGoods> list = shopGoodsService.list(queryWrapper);
        List<String> goodIds = new ArrayList<>();
        list.forEach(item -> goodIds.add(item.getGoodId()));
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("list",goodIds);
    }

    //查询商品状态
    @PostMapping("/goodState")
    public CommonReturn getState(@RequestBody ShopGoods shopGoods){
        LambdaQueryWrapper<ShopGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoods::getShopId,shopGoods.getShopId());
        queryWrapper.eq(ShopGoods::getGoodId,shopGoods.getGoodId());
        ShopGoods good = shopGoodsService.getOne(queryWrapper);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("state",good.getSale());
    }

    //商品切换上架状态
    @PostMapping("/setState")
    public CommonReturn updateGoodState(@RequestBody ShopGoods data){
        LambdaQueryWrapper<ShopGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoods::getShopId,data.getShopId());
        queryWrapper.eq(ShopGoods::getGoodId,data.getGoodId());
        ShopGoods good = shopGoodsService.getOne(queryWrapper);
        int state;
        if(good.getSale()){
            state = 0;
        }else{
            state = 1;
        }
        shopGoodsService.changStateById(good.getId(),state);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    private LambdaQueryWrapper<ShopGoods> buildQueryByShopAndGoodId(ShopGoods shopGoods){
        checkData(shopGoods);
        LambdaQueryWrapper<ShopGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoods::getGoodId,shopGoods.getGoodId());
        queryWrapper.eq(ShopGoods::getShopId,shopGoods.getShopId());
        return queryWrapper;
    }
    private void checkData(ShopGoods shopGoods){
        if(StringUtils.isEmpty(shopGoods.getGoodId())|| StringUtils.isEmpty(shopGoods.getShopId())){
            throw new OperationException(ServiceCode.PARAM_ERROR);
        }
    }
}
